<?php
include_once('config/config.php');
include_once('./config/config.php');
// $admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
  setcookie('auth', '', time() - 1);
  notice('退出成功', 'login.php');
}

// 判断是否有表单提交
if ($_POST) {
  // 接收参数
  $depid = isset($_POST['dep']) ? trim($_POST['dep']) : '';
  $job = isset($_POST['job']) ? trim($_POST['job']) : '';

  // 判断参数是否为空
  if (empty($depid)) {
    notice('部门不能为空');
  }
  if (empty($job)) {
    notice('职位名称不能为空');
  }

  // 组装数据
  $data = [
    'depid' => $depid,
    'name' => $job,
  ];

  $result = add('job', $data);

  if ($result['code'] === 1) {
    notice('添加职位成功', 'JobList.php');
  } else {
    notice('添加职位失败，请重试', 'JobAdd.php');
  }
}



?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>admin</title>
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">

  <!-- 公共样式 -->
  <?php include_once('./common/meta.php'); ?>
  <style>
    #upload {
      width: 214px;
      height: 200px;
      object-fit: cover;
      cursor: pointer;
    }
  </style>

  <!-- jquery -->
  <script src="./assets/lib/jquery-1.7.2.min.js" type="text/javascript"></script>

</head>

<body>
  <!-- 引入头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 侧边菜单栏 -->
  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加职位</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="JobList.php">JobList</a> <span class="divider">/</span></li>
      <li class="active">JobAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='JobList.php'"><i class="icon-list"></i> 职位列表</button>
          <div class="btn-group"></div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <!-- 用户名 -->
                <label>所属部门</label>
                <select name="dep" id="dep">
                  <option value="">请选择</option>
                </select>

                <label>职位名称</label>
                <input type="text" name="job" id="job" placeholder="请输入职位名" class="input-xxlarge">
                <span id="job-error"></span>

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>
</body>

</html>

<?php include_once('common/script.php'); ?>

<script>
  // 发起请求，查询部门
  (() => {
    $.ajax({
      type: 'post',
      url: 'api.php',
      dataType: 'json',
      data: {
        'action': 'select_department'
      },
      success: res => {
        if (res.code === 1) {
          const data = res.data
          let temp = ''
          data.forEach(item => {
            temp += `<option value="${item.id}">${item.name}</option>`
          })
          $('#dep').append(temp)
        } else {
          alert(res.msg)
        }
      }
    })
  })()

  // 根据所选部门————检查用户输入的职位是否已经存在
  $('#job').change(function() {
    const depid = $('#dep').val() // 获取用户所选部门的id
    if (depid) {
      const job = $.trim($(this).val()) // 获取用户输入的职位名
      if (!job) {
        $('#job-error').html('')
        return false
      }

      // 发起请求
      $.ajax({
        type: 'post',
        url: 'api.php',
        data: {
          job,
          depid,
          action: 'check_job_name'
        },
        dataType: 'json',
        success: (result) => {
          if (result.code === 1) {
            $('#job-error').text(result.msg).css({
              color: 'green',
              fontSize: '16px'
            })
          } else {
            $('#job-error').text(result.msg).css({
              color: 'red',
              fontSize: '16px'
            })
          }
        }
      })
    } else {
      alert('请先选择部门')
      $('#job').val('')
    }
  })

  $('#dep').change(() => {
    $('#job').val('')
  })
</script>